﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kardex_Entity;

namespace Kardex_DataAccess
{
    public class PreventistaDA
    {
        bd_kardexEntities context;
        public PreventistaDA()
        {
            context = new bd_kardexEntities();
        }

        public List<person> GetListPreventista()
        {
            try
            {
                var query = from P in context.preventista 
                            from per in context.person 
                            where   per.IdPerson==P.IdPerson
                            orderby per.IdPerson
                            select per;
                var List = query.ToList();
                return List;
            }
            catch (Exception ex)
            {                
               throw new Exception (ex.Message.ToString());
            }
        }
        public person GetObjByIdperson(int IdPerson)
        {
            try
            {
                var q = from pre in context.preventista
                        from p in context.person
                        where p.IdPerson == pre.IdPerson &&
                        pre.IdPerson == IdPerson
                        orderby p.IdPerson
                        select p;
                person objPerson = (person)q.FirstOrDefault();
                return objPerson;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// Save Preventista
        /// </summary>
        /// <param name="Preventista"> Obj Preventista</param>
        /// <returns>Id Of new Preventista</returns>
        public person Save( person NewPrev)
        {
            try
            {
                context.person.AddObject(NewPrev);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return NewPrev;
        }
        /// <summary>
        /// Update Preventista
        /// </summary>
        /// <param name="Preventista"></param>
        /// <returns></returns>
        public bool Udate(person PreventistaModif)
        {
            bool ReturnResult = false;
            try
            {
                context.person.Attach(PreventistaModif);
                context.ObjectStateManager.ChangeObjectState(PreventistaModif, System.Data.EntityState.Modified);
                context.SaveChanges();


                context.preventista.Attach(PreventistaModif.preventista);
                context.ObjectStateManager.ChangeObjectState(PreventistaModif.preventista, System.Data.EntityState.Modified);
                context.SaveChanges();
                ReturnResult = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return ReturnResult;
        }
        /// <summary>
        /// Delete Preventista
        /// </summary>
        /// <param name="Distrib">Obj Preventista</param>
        /// <returns>Is true or FAlse of Delete</returns>
        public bool Delete(person PreventistaDelete)
        {
            bool ReturnResult = false;
            try
            {
                context.person.Attach(PreventistaDelete);
                context.DeleteObject(PreventistaDelete);
                context.SaveChanges();
                ReturnResult = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return ReturnResult;
        }


    }
}
